{% extends '_base_create_update.html' %}

{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">
{% endblock %}


<!-- Content head -->
{% block head_title %}
    系统用户
{% endblock %}
{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">资产管理</a></li>
        <li class="active">系统用户</li>
    </ol>
{% endblock %}

<!-- Content body -->
{% block body_title %}
    新增系统用户
{% endblock %}

{% block body_form %}
    <form name="assetForm" action="" class="form-horizontal">
        <div class="panel-body">
            <p class="bord-btm pad-ver text-main text-bold">基本</p>
            <div class="row">
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>名称</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="name" value="{{ name }}" placeholder="请填写用户名称" data-rule="名称: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>系统用户</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="username" value="{{ username }}" placeholder="请填写系统用户" data-rule="系统用户: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">* </span>登录模式</label>
                            <div class="col-lg-8">
                                <select class="select2 form-control" name="login_mode" data-placeholder="请选择登录模式" data-rule="登录模式: required;">
                                    <option value="auto" {% if login_mode == "auto" %} selected {% endif %}>自动登录</option>
                                    <option value="manual" {% if login_mode == "manual" %} selected {% endif %}>手动登录</option>
                                </select>
                                <span class="fa fa-info-circle mar-top-7"> 如果选择手动登录模式，用户名和密码可以不填写</span>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger"></span>优先级</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="priority" value="{{ priority }}" placeholder="请填写优先级" autocomplete="off">
                                <span class="fa fa-info-circle mar-top-7"> 1-100，1最低优先级，100最高优先级。授权多个用户时，高优先级的系统用户将会作为默认登录用户</span>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>

            <div id="auth">
                <br>
                <p class="bord-btm pad-ver text-main text-bold">认证</p>
                <div class="row">
                    <div class="col-lg-12">
                        <fieldset class="pad-top">
                            <div class="form-group">
                                <label class="col-lg-2 control-label"><span class="text-danger"></span>自动推送</label>
                                <div class="col-lg-8 pad-top-7">
                                    <input id="autoPush" name="auto_push" class="magic-checkbox" type="checkbox" {% if auto_push == 1 %} checked {% endif %}>
                                    <label for="autoPush"></label>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                    <div class="col-lg-12">
                        <fieldset class="pad-top">
                            <div class="form-group">
                                <label class="col-lg-2 control-label"><span class="text-danger"></span>用户密码</label>
                                <div class="col-lg-8">
                                    <input type="password" class="form-control" name="password" value="{{ password }}" placeholder="请填写用户密码" autocomplete="off">
                                </div>
                            </div>
                        </fieldset>
                        <fieldset>
                            <div class="form-group">
                                <div class="col-lg-offset-2 col-lg-8">
                                    <input id="showPasswd" class="magic-checkbox" type="checkbox">
                                    <label for="showPasswd">显示密码</label>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                    <div class="col-lg-12">
                        <fieldset class="pad-top">
                            <div class="form-group">
                                <label class="col-lg-2 control-label"><span class="text-danger"></span>私钥文件</label>
                                <div class="col-lg-8">
                                    <textarea class="form-control" rows="8" name="private_key">{{ private_key }}</textarea>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                </div>
            </div>

            <br>
            <p class="bord-btm pad-ver text-main text-bold">其他</p>
            <div class="row">
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">*</span>Sudo</label>
                            <div class="col-lg-8">
                                <textarea disabled class="form-control" name="sudo" rows="5">{{ sudo }}</textarea>
                                <span class="fa fa-info-circle mar-top-7"> 使用逗号分隔多个命令，如: /bin/whoami,/sbin/ifconfig</span>
                            </div>
                        </div>
                    </fieldset>
                </div>
                <div class="col-lg-12">
                    <fieldset class="pad-top">
                        <div class="form-group">
                            <label class="col-lg-2 control-label"><span class="text-danger">*</span>Shell</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" name="shell" placeholder="请填写用户登录的shell" value="{{ shell }}" data-rule="名称: required;" autocomplete="off">
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>

        </div>

        <div class="panel-footer">
            <div class="row">
                <div class="col-sm-6 col-sm-offset-6">
                    <div class="col-sm-7 col-sm-offset-3">
                        <button class="btn btn-mint pull-right submit-btn" type="submit">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
{% endblock %}



{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>

    <script type="text/javascript">
        $(function () {
            // 显示或者隐藏密码
            $("#showPasswd").on('click', function () {
                var isCheck = $(this).prop('checked');
                if(isCheck){
                    $(this).closest('form').find('input[name="password"]').attr("type", "text");
                }else{
                    $(this).closest('form').find('input[name="password"]').attr("type", "password");
                }
            });
            // 登录模式
            $('select[name="login_mode" ]').on('change', function () {
                var mode = $(this).val();
                if(mode == 'manual'){
                    $("#auth").addClass('hide');
                    $("#autoPush").prop('checked', false);
                    $("#autoGenerateKey").prop('checked', false);
                }else{
                    $("#auth").removeClass('hide');
                    $("#autoPush").prop('checked', true);
                    $("#autoGenerateKey").prop('checked', true);
                }
            }).trigger("change");
        });

        $(function () {
            bees.initSelect2({
            });

            var $formEle = $('form[name="assetForm"]');
            bees.niceValidate($formEle);
            $formEle.on("valid.form", function () {
                setAjaxCSRFToken();

                $.ajax({
                    type: 'post',
                    url: '{{ url_for("assets.systemuser-update", pk=default_pk) }}'.replace(bees.defaultPK, '{{ id }}'),
                    data: {
                        'name': $('input[name="name"]').val(),
                        'username': $('input[name="username"]').val(),
                        'login_mode': $('select[name="login_mode"]').val(),
                        'priority': $('input[name="priority"]').val(),
                        'auto_push': $('input[name="auto_push"]').prop('checked') ? 1 : 0,
                        'auto_generate_key': 0,
                        'password': $('input[name="password"]').val(),
                        'private_key': $('textarea[name="private_key"]').val(),
                        'sudo': $('textarea[name="sudo"]').val(),
                        'shell': $('input[name="shell"]').val()
                    },
                    success: function (result) {
                        if (result.code != 200) {
                            $formEle.find('*[name='+ result.data.field +']').focus().addClass('has-error');
                            bees.tipError({
                                message: result.data.message
                            });
                        }else {
                            bees.tipInfo({
                                message: '更新成功'
                            });
                            window.location.href = '{{ url_for("assets.systemuser-list") }}'
                        }
                    }
                });
            });

        })

    </script>

{% endblock %}
